﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.BLL;
using System.Reflection;
using System.Threading;
using System.Timers;
using ZX.SYS.Bussiness;
using System.Configuration;
namespace ZX.WebApp
{
    public class Global : System.Web.HttpApplication
    {
        //public static readonly string times = ConfigurationManager.AppSettings["MTime"].ToString();
        protected void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            Application["count"] = 0;
            //Application["MqSend"] = 0;
            //SysScheduler.Instance.Start();
            ////this.notifyIcon1.Visible = true;
            //SysTask task = new SysTask();
            //task.GroupName = "Start";
            //task.JobName = "StartTask";
            //task.TaskName = "StartTask";
            //task.TriggerName = "StartTrriger";
            //task.CronExp = "30 5 * ? * *";
            //task.RunFlag = "1";
            //task.JobType = "ZX.BLL.RunIssueTask,ZX.BLL";
            //task.JobStartDate = DateTime.UtcNow;

            //string[] _type = task.JobType.Split(',');
            //if (_type.Length > 1)
            //{
            //    string AssemblyName = _type[1];
            //    string TypeName = _type[0];
            //    Assembly assem = Assembly.Load(AssemblyName);
            //    Type t = assem.GetType(TypeName);
            //    SysScheduler.Instance.Insert(task, t);
            //}


            
        }

        void myTimer_Elapsed(object source, ElapsedEventArgs e)
        {
            try
            {
                if (Application["MqSend"].ToString() == "0")
                {
                        Application["MqSend"] = 1;
                    (new MQproccess()).AutoSendExe();
                        Application["MqSend"] = 0;
                }
            }
            catch (Exception ee)
            {
                Application["MqSend"] = 0;
            }
            try
            {
                (new MQproccess()).AutoRecdExe();
            }
            catch (Exception ee)
            {
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码
            Application.Lock();
            Application["count"] = (int)Application["count"] + 1;
            Application.UnLock();
            //ZX.BLL.DataCacheScheduler.Instance.Start();

 
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected  void Application_Error(object sender, EventArgs e)
        {
            //SysError error = new SysError();
            //// 在出现未处理的错误时运行的代码
            //Exception x = Server.GetLastError().GetBaseException();
            //////将错误写入系统事件日志
            //error.Message = x.Message;
            //error.StackTrace = x.StackTrace;
            //error.Source = x.Source;
            //error.Method = x.TargetSite.Name;


            //Server.ClearError();
            //Application["error"] = error; 
            //BaseBussnies buss = new BaseBussnies();

            //SysDataLog log = new SysDataLog();
            ////DatUser user = (Session["User"] as DatUser);
            ////log.UserID = user==null ? Guid.Empty :user.UserID;
            //log.UserID = Guid.Empty;
            //log.Action = "Login";
            //log.Data = error.ToJSON();
            //log.EntityName = "";
            //log.LogTime = DateTime.Now;
            //buss.entity = log;
            //buss.Insert();
            //定向到错误处理页面
            //Response.Redirect("~/Resources/ShowError.aspx");
        }

        protected void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。  
            Application.Lock();
            Application["count"] = (int)Application["count"] - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {
            //ZX.BLL.SysScheduler.Instance.Stop();
        }
    }
}